package com.naiterui.ehp.bs.doctor.dao.impl;

import com.naiterui.ehp.bp.domain.DoctorCharge;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.utils.lang.StringUtil;
import com.naiterui.ehp.bs.doctor.dao.IDoctorChargeDao;
import com.naiterui.ehp.bs.doctor.repository.IDoctorChargeRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 医生收费信息持久层接口实现
 *
 * @author Amber
 * @since 1.0.0
 */
@Component
public class AdDoctorChargeDaoImpl implements IDoctorChargeDao {

    @Autowired
    private IDoctorChargeRepository doctorChargeRepository;

    @Override
    public Map<Integer, DoctorCharge> getDoctorCharges(Long doctorId) {
        Map<Integer, DoctorCharge> chargeMap = new HashMap<>();
        List<DoctorCharge> charges = doctorChargeRepository.findByDoctorId(doctorId);
        for (DoctorCharge doctorCharge : charges) {
            // 转map
            chargeMap.put(doctorCharge.getChargeType(), doctorCharge);
        }
        return chargeMap;
    }

    @Override
    public DoctorCharge getDoctorChargeByType(Long doctorId, int type) {
        List<DoctorCharge> charges = doctorChargeRepository.findByDoctorIdAndChargeType(doctorId, type);
        return charges.size() == 0 ? null : charges.get(0);
    }

    @Override
    public void saveOrUpdate(DoctorCharge doctorCharge) throws BusinessException {
        if(doctorCharge.getId() == null){
            doctorChargeRepository.save(doctorCharge);
        } else {
            doctorChargeRepository.update(doctorCharge);
        }
    }

    /**
     * 获取指定医生开通服务信息集合
     *
     * @param doctorIdList 医生id列表
     * @param type         收费服务类型（1：图文；2：电话；4：视频）
     *
     * @return
     */
    @Override
    public Map<Long, DoctorCharge> getChargeMap(List<Long> doctorIdList, int type) {
        Map<Long, DoctorCharge> chargeMap = new HashMap<>();
        String drIds = StringUtil.convertListToStr(doctorIdList);
        if (StringUtils.isNotEmpty(drIds)) {
            List<DoctorCharge> charges = doctorChargeRepository.findByDoctorIdInAndChargeType(doctorIdList, type);
            for (DoctorCharge charge : charges) {
                chargeMap.put(charge.getDoctorId(), charge);
            }
        }
        return chargeMap;
    }

    @Override
    public DoctorCharge findFirstByDoctorIdAndChargeType(Long doctorId, Integer chargeType) {
        return doctorChargeRepository.findFirstByDoctorIdAndChargeType(doctorId, chargeType);
    }
}
